package com.yanqu.road.dao.impl.channelcenter.voucher;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.voucher.config.VoucherConfigInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class VoucherConfigInfoDaoImpl extends TempDataObjectDao<VoucherConfigInfo> {

    public static String getVoucherConfigInfoMapSql = "select * from `t_n_voucher` where userId = 0 ";

    public Map<Integer, List<VoucherConfigInfo>> getVoucherConfigInfoMap() {
        Map<Integer, List<VoucherConfigInfo>> map = new ConcurrentHashMap<>();

        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getChannelServerDb());
            call = getDb().getConnection().prepareCall(getVoucherConfigInfoMapSql);
            rs = call.executeQuery();
            while (rs.next()) {
                VoucherConfigInfo data = parse(rs);
                List<VoucherConfigInfo> voucherConfigInfoList = map.getOrDefault(data.getType(), new ArrayList<>());
                voucherConfigInfoList.add(data);
                map.put(data.getType(), voucherConfigInfoList);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    /**
     * 不提供插入
     */
    @Override
    protected boolean addData(VoucherConfigInfo data) {
        return true;
    }

    public static String updateVoucherConfigInfoSql = "update `t_n_voucher` set userId=?,useTime=? where voucherId=?";

    @Override
    protected boolean updateData(VoucherConfigInfo data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.DATE, new java.util.Date(data.getUseTime())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getVoucherId()));
        if (getDb().executeNoneQuery(updateVoucherConfigInfoSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected VoucherConfigInfo parseData(ResultSet rs) throws SQLException {
        VoucherConfigInfo data = new VoucherConfigInfo();
        data.setVoucherId(rs.getLong("voucherId"));
        data.setCode(rs.getString("code"));
        data.setType(rs.getInt("type"));
        data.setPrice(rs.getInt("price"));
        data.setChannelId(StringUtils.stringToIntegerList(rs.getString("channelId"), ","));
        data.setStartTime(rs.getDate("startTime").getTime());
        data.setEndTime(rs.getDate("endTime").getTime());
        data.setUserId(rs.getLong("userId"));
        Date useTime = rs.getDate("useTime");
        if (useTime != null) {
            data.setUseTime(useTime.getTime());
        }
        return data;
    }
}
